package priv.mill.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpSimpleServer {

	public static void main(String[] args) {
		try {
			ServerSocket socket = new ServerSocket(8881);
			Socket accept = socket.accept();
			InputStream inputStream = accept.getInputStream();
			int available = inputStream.available();//获取有效数据
			System.out.println(available);
			byte[] b = new byte[4096];
			int i = 0;//总大小用于判断是否读完
			int len;//每次读取大小，用于获取当前所读取的数据
			while((i+=len=inputStream.read(b))<=available) {
				System.out.println(new String(b,0,len));
				if(i>=available)break;
			}
			accept.shutdownInput();
			OutputStream outputStream = accept.getOutputStream();
			outputStream.write("HTTP/1.1 200 OK\r\n".getBytes());
			outputStream.write("Content-Type:text/html;charset=utf-8\r\n".getBytes());
			outputStream.write("\r\n".getBytes());
			outputStream.write("<h1>这是我的服务消息</h1>".getBytes());
			accept.shutdownOutput();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
